package com.sitech.portal.system.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.JSONUtil;
/**
 * 帮助信息统一action
 * @author zhangdajun
 *
 */
public class PortalHelpAttachAction extends JGridBaseAction {

	private File imgFile;

	private String imgFileFileName;

	private String imgFileContentType;
	
	private String attachContextPath;

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getAttachContextPath() {
		return attachContextPath;
	}

	public void setAttachContextPath(String attachContextPath) {
		this.attachContextPath = attachContextPath;
	}

	@Override
	public String execute() throws Exception {
		String savePath = this.getRequest().getSession().getServletContext().getRealPath("/") + attachContextPath;
		// 文件保存目录URL
		String saveUrl = this.getRequest().getContextPath() + File.separator + attachContextPath;
		String dirName = this.getRequest().getParameter("dir");
		if (dirName == null) {
			dirName = "image";
		}
		String tpath = FileUtil.mkdirs(savePath, dirName);
		savePath += tpath;
		saveUrl += tpath;
		String newFileName = FileUtil.createNewFileName(imgFileFileName);
		InputStream in = new FileInputStream(imgFile);
		OutputStream fos = new FileOutputStream(savePath + newFileName);
		byte[] buffer = new byte[1024 * 1024];
		int length = 0;
		while (-1 != (length = in.read(buffer))) {
			fos.write(buffer, 0, length);
		}
		fos.close();
		in.close();

		HashMap<String,Object> obj = new HashMap<String,Object>();
		obj.put("error", 0);
		obj.put("url", saveUrl + newFileName);
		this.setJSON(JSONUtil.toJSON(obj));
		return SUCCESS;
	}

	static class FileUtil {

		public static String createNewFileName(String fileName) {

			String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
					.toLowerCase();

			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String newFileName = df.format(new Date()) + "_"
					+ new Random().nextInt(1000) + "." + fileExt;
			System.out.println(newFileName);

			return newFileName;
		}

		public static String mkdirs(String basePath, String dirName) {

			basePath += dirName + "/";
			File saveDirFile = new File(basePath);
			if (!saveDirFile.exists()) {
				saveDirFile.mkdirs();
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());
			basePath += ymd + "/";
			File dirFile = new File(basePath);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			return dirName + "/" + ymd + "/";
		}
	}

}
